// index.js
// 获取应用实例
import {
	user,
	form
} from '../../api/apiData'
Page({
	data: {
		info:'',
		id:1
	},

	onLoad(option) {
		if(option.id){
			this.setData({
				id:option.id
			})
		}
		if(option.value){
			this.setData({
				val:option.value
			})
		}
		this.getUserInfos()
	},
	toUrl() {
		if(this.data.val){
			wx.navigateTo({
				url: '/pages/info/index?id='+this.data.id+'&val='+this.data.val,
			})
		}else{
			wx.navigateTo({
				url: '/pages/info/index?id='+this.data.id,
			})
		}

	},
	/**
	 * 授权登录
	 */
	getUserInfos(e) {
		wx.showLoading({
			title: "正在加载中...",
		})
		const that = this;
		wx.login({
			success(res) {
				let code = res.code
				wx.getUserInfo({
					success: (res) => {
						wx.hideLoading();
						var encryptedData = res.encryptedData;
						var iv = res.iv
						var info = res.rawData
						var obj = {
							code: code,
							iv: iv,
							encryptedData: encryptedData
						}
						user.wxlogin(obj, res => {
							if (res.code == 1) {
								wx.setStorage({
									key: "token",
									data: 'Dearer '+res.data.token
								})
								that.getForm()
							} else {
								wx.showToast({
									title: res.msg,
									icon: 'none'
								})
							}
						})
					},
					fail: res => {
						wx.hideLoading();
						console.log(res)

					}
				})
			}
		})
	},
	getForm(){
		wx.showLoading({
			title: "正在加载中...",
		})
		form.detail({form_id:this.data.id},res=>{
			wx.hideLoading()
			if(res.code==1){
				this.setData({
					info:res.data
				})
			}else{
				wx.showToast({
					title: res.msg,
					icon:'error'
				})
			}
			
		})
	},
	onShow() {},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})